package me.zhengjie.modules.blog.service.impl;

import me.zhengjie.exception.BadRequestException;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.exception.EntityNotFoundException;
import me.zhengjie.modules.blog.domain.BlogUser;
import me.zhengjie.modules.blog.repository.BlogUserRepository;
import me.zhengjie.modules.blog.service.BlogUserService;
import me.zhengjie.modules.blog.service.dto.BlogUserDTO;
import me.zhengjie.modules.blog.service.mapper.BlogUserMapper;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author zhangqiang
 * @date 2019-03-01
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BlogUserServiceImpl implements BlogUserService {

    @Autowired
    private BlogUserRepository blogUserRepo;

    @Autowired
    private BlogUserMapper blogUserMapper;

    @Override
    public BlogUserDTO findById(long id) {
        Optional<BlogUser> blogUser = blogUserRepo.findById(id);
        ValidationUtil.isNull(blogUser,"BlogUser","id",id);
        return blogUserMapper.toDto(blogUser.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BlogUserDTO create(BlogUser resources) {

        if(blogUserRepo.findByUsername(resources.getUsername())!=null){
            throw new EntityExistException(User.class,"username",resources.getUsername());
        }

        if(blogUserRepo.findByEmail(resources.getEmail())!=null){
            throw new EntityExistException(User.class,"email",resources.getEmail());
        }

        // 默认密码 123456，此密码是 MD5加密后的字符
        resources.setPassword("14e1b600b1fd579f47433b88e8d85291");
        resources.setAvatar("https://i.loli.net/2018/12/06/5c08894d8de21.jpg");
        return blogUserMapper.toDto(blogUserRepo.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BlogUser resources) {

        /**
         * 根据实际需求修改
         */
        if(resources.getId().equals(1L)){
            throw new BadRequestException("该账号不能被修改");
        }

        Optional<BlogUser> userOptional = blogUserRepo.findById(resources.getId());
        ValidationUtil.isNull(userOptional,"User","id",resources.getId());

        BlogUser blogUser = userOptional.get();

        BlogUser blogUser1 = blogUserRepo.findByUsername(blogUser.getUsername());
        BlogUser blogUser2 = blogUserRepo.findByEmail(blogUser.getEmail());


        if(blogUser1 !=null&&!blogUser.getId().equals(blogUser1.getId())){
            throw new EntityExistException(User.class,"username",resources.getUsername());
        }

        if(blogUser2!=null&&!blogUser.getId().equals(blogUser2.getId())){
            throw new EntityExistException(User.class,"email",resources.getEmail());
        }

        blogUser.setUsername(resources.getUsername());
        blogUser.setEmail(resources.getEmail());
        blogUser.setStatus(resources.getStatus());

        blogUserRepo.save(blogUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        /**
         * 根据实际需求修改
         */
        if(id.equals(1L)){
            throw new BadRequestException("该账号不能被删除");
        }
        blogUserRepo.deleteById(id);
    }

    @Override
    public BlogUser findByName(String userName) {
        BlogUser blogUser = null;
        if(ValidationUtil.isEmail(userName)){
            blogUser = blogUserRepo.findByEmail(userName);
        } else {
            blogUser = blogUserRepo.findByUsername(userName);
        }

        if (blogUser == null) {
            throw new EntityNotFoundException(BlogUser.class, "name", userName);
        } else {
            return blogUser;
        }
    }
}
